from app01 import models
from django.core.exceptions import ValidationError
from django import forms
from app01.utils.bootstrap import BootStrapModelForm
from app01.utils.encrypt import md5


class UserLoginModelForm(BootStrapModelForm):
    class Meta:
        model = models.User
        fields = ["email", "password"]
        widgets = {
            "password": forms.PasswordInput(),
        }

    def clean_email(self):
        txt_email = self.cleaned_data.get("email")
        if txt_email is None:
            raise ValidationError("邮箱不可为空")
        exists = models.User.objects.filter(email=txt_email).exists()
        if not exists:
            raise ValidationError("该邮箱未注册")
        return txt_email

    def clean_password(self):
        txt_email = self.cleaned_data.get("email")
        txt_password = self.cleaned_data.get("password")
        if txt_password is None:
            raise ValidationError("密码不可为空")
        md5_txt_password = md5(txt_password)
        exists = models.User.objects.filter(email=txt_email).exists()
        if exists:
            if models.User.objects.filter(email=txt_email).first().password != md5_txt_password:
                raise ValidationError("密码错误")
        return md5_txt_password


class UserRegisterModelForm(BootStrapModelForm):
    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput(),
        required=False
    )

    class Meta:
        model = models.User
        fields = ["name", "email", "password", "confirm_password"]
        widgets = {
            "password": forms.PasswordInput(),
        }

    def clean_email(self):
        txt_email = self.cleaned_data.get("email")
        if txt_email is None:
            raise ValidationError("邮箱不可为空")
        exists = models.User.objects.filter(email=txt_email).exists()
        if exists:
            raise ValidationError("该邮箱已注册")
        return txt_email

    def clean_name(self):
        txt_name = self.cleaned_data.get("name")
        if txt_name is None:
            raise ValidationError("用户名不可为空")
        exists = models.User.objects.filter(name=txt_name).exists()
        if exists:
            raise ValidationError("该用户名已存在")
        return txt_name

    def clean_password(self):
        txt_password = self.cleaned_data.get("password")
        if txt_password is None:
            raise ValidationError("密码不可为空")
        return md5(txt_password)

    def clean_confirm_password(self):
        txt_password = self.cleaned_data.get("password")
        confirm = md5(self.cleaned_data.get("confirm_password"))
        if txt_password is None:
            return confirm
        if confirm != txt_password:
            raise ValidationError("密码不一致")
        return confirm


class UserEditModelForm(BootStrapModelForm):
    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput(render_value=True),
        required=False
    )

    class Meta:
        model = models.User
        fields = ["email", 'password', 'confirm_password']
        widgets = {
            "password": forms.PasswordInput(render_value=True)
        }

    def clean_email(self):
        txt_email = self.cleaned_data["email"]
        if txt_email is None:
            raise ValidationError("邮箱不可为空")
        exists = models.User.objects.filter(email=txt_email).exists()
        if not exists:
            raise ValidationError("该邮箱未注册")
        return txt_email

    def clean_password(self):
        txt_password = self.cleaned_data.get("password")
        if txt_password is None:
            raise ValidationError("密码不可为空")
        md5_password = md5(txt_password)
        exists = models.User.objects.filter(id=self.instance.pk, password=md5_password).exists()
        if exists:
            raise ValidationError("不能与以前的密码相同")

        return md5_password

    def clean_confirm_password(self):
        confirm = md5(self.cleaned_data.get("confirm_password"))
        txt_password = self.cleaned_data.get("password")
        if txt_password is None:
            return confirm
        if confirm != txt_password:
            raise ValidationError("密码不一致")
        return confirm


class AddArticle(BootStrapModelForm):
    class Meta:
        model = models.Article
        fields = ["title", 'abstract', 'content']

    def clean_title(self):
        txt_title = self.cleaned_data["title"]
        if txt_title is None:
            raise ValidationError("不可为空")
        return txt_title

    def clean_abstract(self):
        txt_abstract = self.cleaned_data["abstract"]
        if txt_abstract is None:
            raise ValidationError("不可为空")
        return txt_abstract

    def clean_content(self):
        txt_content = self.cleaned_data["content"]
        if txt_content is None:
            raise ValidationError("不可为空")
        return txt_content


class AddTieZi(BootStrapModelForm):
    class Meta:
        model = models.TieZi
        fields = ["title", 'content']

    def clean_title(self):
        txt_title = self.cleaned_data["title"]
        if txt_title is None:
            raise ValidationError("不可为空")
        return txt_title

    def clean_content(self):
        txt_content = self.cleaned_data["content"]
        if txt_content is None:
            raise ValidationError("不可为空")
        return txt_content

